//
//  ForumDepartmentViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-17.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "ForumDepartmentViewController.h"


static ASIHTTPConnection *forumlist_connect;

@interface ForumDepartmentViewController ()

@end

@implementation ForumDepartmentViewController


- (void)dealloc{
    
    if (forumlist_connect != nil) {
        forumlist_connect.connectionDelegate = nil;
        [forumlist_connect release];
        forumlist_connect = nil;
    }
    
    [myArray release];
    [myTableView release];
    
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithSectionId:(NSString *)sectionId{
    
    self = [super init];
    if (self) {
        departmentId = sectionId;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (myArray == nil) {
        myArray = [[NSMutableArray alloc]init];
    }
    
    // Do any additional setup after loading the view from its nib.
    [self  loadForumListData];
    
}


- (void)loadForumListData{
    
    NSString *urlString = [NSString stringWithFormat:@"%@/forumHomePage?SectionID=%@",UCENTERBASEURL,departmentId];
	NSURL *url = [NSURL URLWithString:urlString];
    
    if (forumlist_connect != nil) {
        forumlist_connect.connectionDelegate = nil;
        [forumlist_connect release];
        forumlist_connect = nil;
    }
    
    forumlist_connect = [[ASIHTTPConnection alloc] initGETWithURL:url resultDict:YES isCache:YES];
    forumlist_connect.connectionDelegate = self;
    
    [forumlist_connect startAsynchronous];

    
}

#pragma mark connectino delegate
- (void)requestFinished:(ASIHTTPConnection *)connection resultDict:(NSDictionary *)dict URL:(NSURL *)url info:(NSDictionary *)info{
    
    NSArray *array = [dict objectForKey:@"forumpostlist"];
    
    if (myArray != nil) {
        [myArray removeAllObjects];
    }
    
    [myArray addObjectsFromArray:array];
    
    [myTableView reloadData];
    
}

- (void)requestFailed:(ASIHTTPConnection *)connection error:(NSError *)error URL:(NSURL *)url info:(NSDictionary *)info{
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MyIdentifier = @"forumCell";
    
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell =(UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] init] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
        [titleLable setTextAlignment:NSTextAlignmentLeft];
        titleLable.numberOfLines = 2;
        [titleLable setTextColor:[UIColor blackColor]];
        titleLable.backgroundColor = [UIColor clearColor];
        titleLable.tag = 100;
        [cell addSubview:titleLable];
        
        [titleLable release];
        
        
        UILabel *author = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 150, 20)];
        [author setTextAlignment:NSTextAlignmentLeft];
        [author setTextColor:[UIColor grayColor]];
        author.font = [UIFont systemFontOfSize:14];
        author.backgroundColor = [UIColor clearColor];
        author.tag = 101;
        [cell addSubview:author];
        
        [author release];
        
        
        UILabel *date = [[UILabel alloc]initWithFrame:CGRectMake(160, 50, 155, 20)];
        [date setTextAlignment:NSTextAlignmentLeft];
        [date setTextColor:[UIColor grayColor]];
        date.font = [UIFont systemFontOfSize:14];
        date.backgroundColor = [UIColor clearColor];
        date.tag = 102;
        [cell addSubview:date];
        
        [date release];

        
    }
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    
    NSString *subject = [dict objectForKey:@"subject"];
    
    UILabel *lable = (UILabel *)[cell viewWithTag:100];
    lable.text = subject;
    
    NSString *author = [dict objectForKey:@"author"];
    UILabel *lable1 = (UILabel *)[cell viewWithTag:101];
    lable1.text = [NSString stringWithFormat:@"作者：%@",author];
    
    NSString *date = [dict objectForKey:@"postDate"];
    UILabel *lable2 = (UILabel *)[cell viewWithTag:102];
    lable2.text = date;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
    NSString *postId = [dict objectForKey:@"postID"];
    
    ForumPostDetailViewController *viewController = [[ForumPostDetailViewController alloc]initwithPostId:postId];
    
        
    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
    
    
}


@end
